// const Koa = require('koa')
const Koa = require('./3.0/myKoa')

const app = new Koa()

let sleep = (num = 2000) =>  new Promise((resolve) => {
    setTimeout(() => {
        resolve()
    }, num)
})

let logger = async (ctx, next) => {
    const start = Date.now()
    console.log(`start: ${start}`)
    await next()
    const end = Date.now()
    console.log(`请求 ${ctx.url}, 耗时: ${end - start}`)
}

app.use(logger)

app.use(async (ctx, next) => {
    ctx.body = '1'
    await sleep()
    await next()
    ctx.body += '2'
})

app.use(async (ctx, next) => {
    ctx.body += '3'
    await next()
    ctx.body += '4'
})

app.use(async (ctx, next) => {
    ctx.body += '5'
})

// 监听端口
app.listen(3000, () => {
    console.log('server is runing http://localhost:3000')
})
